<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\QuoteFenqiDetail;

Class FenqiController extends AdminController{
    
    public function alist(Request $request){
//         $fliter = QuoteFenqi::where('id','>', 0);
        $name = trim($request->input('name', ''));
        $phone = trim($request->input('phone', ''));
        $idcard = trim($request->input('card_id', ''));
        $status = trim($request->input('status', ''));
        
        
        $page = trim($request->input('page', 0)); // 页码
        $pagenumber = trim($request->input('pagenumber', '10')); // 分页个数
        
        $sql = "select a.*,b.realname, b.card_id, b.phone from mega_quote_fenqi as a left join mega_realname as b on a.member_id = b.member_id where 1 ";
        $sqlcount = "select count(a.id) as count from mega_quote_fenqi as a left join mega_realname as b on a.member_id = b.member_id where 1 ";
        $param = array();
        $sqlwhere = '';
        if ($idcard) {
            $sqlwhere .= " and b.card_id=:card_id ";
            $param[':card_id'] = $idcard;
        }
        if ($name) {
            $name = "%$name%";
            $sqlwhere .= " and b.realname like :realname ";
            $param[':realname'] = $name;
        }
        if ($phone) {
            $sqlwhere .= " and b.phone=:phone ";
            $param[':phone'] = $phone;
        }
        
        if ($status == 1) {
            $sqlwhere .= " and a.is_complate = 1 "; 
        }
        $start = $page * $pagenumber;
        $limit = " limit $start, $pagenumber ";
        $results = DB::select($sql . $sqlwhere . $limit, $param);
        $count = DB::select($sqlcount . $sqlwhere, $param);
//         var_dump($results);exit;
        $items =array();
        foreach ($results as $item){
            $item = (array)$item;// json_decode(json_encode($item), true);
            $items[] = array(
                'id'=>$item['id'],
                'name'=>$item['realname'],
                'card_id'=>$item['card_id'],
                'phone'=>$item['phone'],
                'amount_total'=>$item['amount_total'], //分期金额
                'period'=>$item['period'],  //期数
                'every_amount'=>sprintf('%.02f', ($item['amount_total'] + $item['fee_total']) / $item['period']),  //每期还款额
                'period_num'=>$item['period_num'],  //已还期数
                'created_at'=>$item['created_at'],  //分期时间
                'first'=>date("Y-m-10",  strtotime($item['created_at']." + 30 day")),  //首次还款日
                'status'=>$item['is_complate'],  //状态 已还清，0否，1是
            );
        }
        $data = array(
            'count' => $count[0]->count,
            'items' => $items,
            'page' => $page
        );
        
        $this->output($data);
        
    }
    
    public function detail(Request $request){
        $id = trim($request->input('id', ''));
        $details = QuoteFenqiDetail::where('fenqi_id', $id)->select('number_current', 'amount', 'bill_year', 'bill_month', 'is_complate', 'updated_at')->get();
        //////
        $this->output($details);
    }
    
    
    
    
}